import { Service } from 'egg';

class ManagerService extends Service {
  async findManagerById(id: number) {
    const foundManager = await this.ctx.model.Manager.findByPk(id);
    return foundManager;
  }

  async findManagerByUuid(uuid: string) {
    const foundManager = await this.ctx.model.Manager.findOne({
      where: { uuid }
    });
    return foundManager;
  }

  async findManagerByNameAndPassword(name: string, password: string) {
    const foundManager = await this.ctx.model.Manager.findOne({
      where: { name, password }
    });
    return foundManager;
  }

  async findPasswordByUuid(uuid: string) {
    const foundManager = await this.ctx.model.Manager.findOne({
      where: { uuid }
    });
    return foundManager.password;
  }

  async changePassword(
    uuid: string,
    password: string,
  ) {
    const changedPassword = await this.ctx.model.Manager.update({
      password
    }, { where: { uuid } });

    if (changedPassword > 0) {
      return true;
    }

    return false;
  }
}

module.exports = ManagerService;
